/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-2-3 下午5:52 Created by lyyang
 */
package com.jade.bss.web.resolver;

import com.jade.bss.admin.account.AdminAccount;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

/**
 * 管理员参数解析器
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-1-21 下午4:55
 */
public class AdminArgumentResolver implements WebArgumentResolver
{
    private Log logger = LogFactory.getLog(this.getClass());
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest nativeWebRequest)
            throws Exception
    {
        if (methodParameter.getParameterType().equals(AdminCapsule.class)) {

            Subject subject = SecurityUtils.getSubject();
            Object principal = subject.getPrincipal();
            if (principal instanceof AdminAccount){
                AdminCapsule capsule = new AdminCapsule((AdminAccount)principal);
                return capsule;
            }
            else{
                logger.warn("wrong principal type,required AdminAccount,but "+subject.getPrincipal());
                return new AdminCapsule(new AdminAccount());
            }
        }
        return UNRESOLVED;
    }
}